/* @(#)PerspectiveFragment.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fwb.perspective;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.flywheelms.library.R;
import com.flywheelms.library.gcg.interfaces.GcgTreeView;
import com.flywheelms.library.gcg.treeview.GcgTreeStateMediator;
import com.flywheelms.library.gcg.treeview.GcgTreeViewAdapter;
import com.flywheelms.library.gcg.widget.GcgIncrementalStateButton;
import com.flywheelms.library.gcg.widget.GcgSpinnableMenu;

public abstract class PerspectiveFragment extends Fragment {
	
	public static final String bundle_arg_PAGE_NUMBER = "page_number";
	public static final String bundle_arg_TITLE = "title";

	protected static final String LOGGING_TAG = PerspectiveFragment.class.getSimpleName();
	protected Activity activity;
	protected GcgTreeView treeView;
	protected GcgTreeStateMediator treeStateMediator = null;
	protected GcgTreeViewAdapter treeViewAdapter;
	protected boolean collapsible;
	private GcgSpinnableMenu rightMenu;

	protected PerspectiveFragment(Activity anActivity, LinearLayout aMenuFrameLayout) {
		super();
		this.activity = anActivity;
		initRightMenu(aMenuFrameLayout);
	}
	
	public GcgSpinnableMenu getRightMenu() {
		return this.rightMenu;
	}
	
	private void initRightMenu(LinearLayout aMenuLayout) {
		int[] theMenuBodyResourceIdArray = getRightMenuBodyResourceIdArray();
		this.rightMenu = new GcgSpinnableMenu(
				this.activity,
				aMenuLayout,
				GcgSpinnableMenu.DECORATORS_RIGHT,
				getRightMenuHeadingSpinnerResourceId(),
				R.array.perspective__right_menu__headings,
				theMenuBodyResourceIdArray );
	}
	
	protected static int getRightMenuHeadingArrayResourceId() {
		 return R.array.perspective__right_menu__headings;
	}

	protected abstract int[] getRightMenuBodyResourceIdArray();

	protected static void setPerspectiveArguments(PerspectiveFragment aPerspectiveFragment, int aPageNumber,
			String aTitle) {
		Bundle theBundle = new Bundle(2);
		theBundle.putInt(bundle_arg_PAGE_NUMBER, aPageNumber);
		theBundle.putString(bundle_arg_TITLE, aTitle);
		aPerspectiveFragment.setArguments(theBundle);
	}

	@Override
	public View onCreateView(LayoutInflater anInflater, ViewGroup aContainer,
			Bundle aSavedInstanceState) {
		boolean isNewCollapsible;
		if (aSavedInstanceState == null) {  // new/default tree state
			this.treeStateMediator = createTreeStateMediator();
			isNewCollapsible = true;
		} else {  // restore previous tree state
			this.treeStateMediator = (GcgTreeStateMediator) aSavedInstanceState
					.getSerializable("treeManager");
			isNewCollapsible = aSavedInstanceState.getBoolean("collapsible");
		}
		return initializeTreeView(anInflater, isNewCollapsible);
	}

	protected abstract int getPerspectiveLayoutResourceId();

	private View initializeTreeView(LayoutInflater anInflater,
			boolean isNewCollapsible) {
		View theTreeViewLayout = anInflater.inflate(R.layout.tree_view__layout, null);
		this.treeView = (GcgTreeView) theTreeViewLayout.findViewById(R.id.treeView);
		this.treeViewAdapter = new GcgTreeViewAdapter(
				this.getActivity(), this.treeStateMediator, getFontSizeArray() );
		this.treeView.setAdapter(this.treeViewAdapter);
		setCollapsible(isNewCollapsible);
		return theTreeViewLayout;
	}
	
	protected abstract GcgTreeStateMediator createTreeStateMediator();

	public abstract int[] getFontSizeArray();
	
	public GcgTreeStateMediator getTreeStateMediator() {
		return this.treeStateMediator;
	}

	public GcgTreeViewAdapter getTreeViewAdapter() {
		return this.treeViewAdapter;
	}

	public int getNumberOfLevels() {
		return getFontSizeArray().length;
	}
	
	public boolean isCollapsible() {
		return this.collapsible;
	}
	
	public void setCollapsible(boolean collapsible) {
		this.collapsible = collapsible;
	}
	
	public LinearLayout getRightMenuFrameLayout() {
		return getRightMenu().getMenuFrameLayout();
	}

	@Override
	public void onSaveInstanceState(final Bundle outState) {
        outState.putSerializable("treeManager", this.treeStateMediator);
        outState.putBoolean("collapsible", this.collapsible);
        super.onSaveInstanceState(outState);
    }
	
	abstract protected int getRightMenuHeadingSpinnerResourceId();
	
	abstract protected int getShowMenuLinearLayoutResourceId();
	
	abstract protected int getButtonShowNodeChildSummaryResourceId();
	
	abstract protected int getButtonShowNodeQualityResourceId();
	
	abstract protected int getButtonShowNodeCompletionResourceId();
	
	abstract protected int getButtonShowCollapseToTreeLevelResourceId();
	
	abstract protected int getFilterMenuLinearLayoutResourceId();
	
	abstract protected int getButtonFilterNodesForSponsorResourceId();

	public boolean isShowNodeChildSummary() {
		GcgIncrementalStateButton theMultiStateButton =
				(GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeChildSummaryResourceId());
		if(theMultiStateButton != null && !theMultiStateButton.isOff()) {
			return true;
		}
		return false;
	}

	public boolean isFilterNodesForSponsor() {
		ToggleButton theToggleButton =
				(ToggleButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonFilterNodesForSponsorResourceId());
		return theToggleButton.isChecked();
	}

	public int getShowNodeChildSummaryLevel() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeChildSummaryResourceId());
		return theMultiStateButton.getState();
	}

	public boolean isShowAllNodeCompletions() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeCompletionResourceId());
		if(theMultiStateButton != null && theMultiStateButton.isOn()) {
			return true;
		}
		return false;
	}

	public int getShowNodeCompletionLevel() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeCompletionResourceId());
		return theMultiStateButton.getState();
	}

	public boolean isShowNodeQuality() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeQualityResourceId());
		if(theMultiStateButton != null && !theMultiStateButton.isOff()) {
			return true;
		}
		return false;
	}

	public int getShowNodeQualityLevel() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowNodeQualityResourceId());
		return theMultiStateButton.getState();
	}

	public int getShowCollapseToTreeLevel() {
		GcgIncrementalStateButton theMultiStateButton = (GcgIncrementalStateButton) this.rightMenu.getMenuFrameLayout().findViewById(getButtonShowCollapseToTreeLevelResourceId());
		int theState = theMultiStateButton.getState();
		int theMaxStates = theMultiStateButton.getMaxStates();
		int theTreeLevel = 0;
		switch (theState) {
		case 1:
			theTreeLevel = 99;
			break;
		case 2:
			theTreeLevel = theMaxStates - 1;
			break;
		case 3:
			theTreeLevel = theMaxStates - 2;
			break;
		case 4:
			theTreeLevel = theMaxStates - 3;
			break;
		case 5:
			theTreeLevel = theMaxStates - 4;
			break;
		case 6:
			theTreeLevel = theMaxStates - 5;
			break;
		case 7:
			theTreeLevel = theMaxStates - 6;
			break;
		default:
		}
		return theTreeLevel;
	}

}
